﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

public class FollowAction : MonoBehaviour {

    private Vector3 viewingTarget;   // The position the camera is trying to get to.

    public float followMargin = 0.15f;  // The size of the margin of the viewport. If the viewingTarget is in or beyond the margin, move in its direction.
    public float followSpeed = 10.0f;

    private float minScreenX = 0.0f;
    private float maxScreenX = 0.0f;
    private float minScreenY = 0.0f;
    private float maxScreenY = 0.0f;

    private RemainInBounds remainInBounds;

	// Use this for initialization
	void Start () {

        remainInBounds = GetComponent<RemainInBounds>();

        minScreenX = Screen.width * followMargin;
        maxScreenX = Screen.width * (1 - followMargin);
        minScreenY = Screen.height * followMargin;
        maxScreenY = Screen.height * (1 - followMargin);

        print("Limits are X " + minScreenX + " to " + maxScreenX + ", Y " + minScreenY + " to " + maxScreenY);

        viewingTarget = this.transform.position;
	}

	// Update is called once per frame
	void Update () {

        Vector3 target = FollowActionPriorityQueue.Instance.GetHighestFollowPosition(Time.deltaTime);
        viewingTarget = Vector3.MoveTowards(viewingTarget, target, followSpeed * Time.deltaTime);
        remainInBounds.desiredPosition = viewingTarget;

        Vector3 viewingTargetScreenSpace = camera.WorldToScreenPoint(viewingTarget);
        //print("Target is at " + viewingTargetScreenSpace + " in screen space");
        float xAdjust = 0;
        float yAdjust = 0;

        if (viewingTargetScreenSpace.x < minScreenX)
        {
            xAdjust = viewingTargetScreenSpace.x - minScreenX;
            //print("Target is to the left of viable area, adjusting by " + xAdjust + " in screen space");
        }
        else if (viewingTargetScreenSpace.x > maxScreenX)
        {
            xAdjust = viewingTargetScreenSpace.x - maxScreenX;
            //print("Target is to the right of viable area, adjusting by " + xAdjust + " in screen space");
        }

        if (viewingTargetScreenSpace.y < minScreenY)
        {
            yAdjust = viewingTargetScreenSpace.y - minScreenY;
            //print("Target is below viable area, adjusting by " + yAdjust + " in screen space");
        }
        else if (viewingTargetScreenSpace.y > maxScreenY)
        {
            yAdjust = viewingTargetScreenSpace.y - maxScreenY;
            //print("Target is above viable area, adjusting by " + yAdjust + " in screen space");
        }

        // Adjustment vector represents the vector we can move to get the target back into the viable area.
        // The only trouble is that it's in screen coordinates.
        Vector3 adjust = new Vector3(xAdjust, yAdjust, 0);

        Vector3 currentScreenSpacePosition = viewingTargetScreenSpace;
        Vector3 targetScreenSpacePosition = viewingTargetScreenSpace + adjust;

        Vector3 currentWorldSpacePosition = camera.ScreenToWorldPoint(viewingTargetScreenSpace);
        Vector3 targetWorldSpacePosition = camera.ScreenToWorldPoint(targetScreenSpacePosition);

        Vector3 worldAdjustmentVector = targetWorldSpacePosition - currentWorldSpacePosition;
        worldAdjustmentVector.z = 0;


        //if (adjust != Vector3.zero) print("Adjusting to follow target at " + viewingTarget + " by moving from " + transform.position + " adjusting by " + worldAdjustmentVector);

        transform.position += worldAdjustmentVector;

	}
}
